/*
 * GChart.java
 * Created on 27 janv. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.misc;


import java.util.GregorianCalendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.club.GClub;
import com.hockeo.vhbase.data.dao.season.GMatchday;
import com.hockeo.vhbase.data.helper.GChartType;

/**
 * Represents a chart that is available in the database.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "t_chart" )
public class GChart implements IGEntity
{
  /**
   * Unique image id.
   */
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int               d_nId = 0;

  /**
   * Binary image data.
   */
  @Column( name = "data" )
  private byte[]            d_data;

  /**
   * The image's content type, e.g. 'image/png'.
   */
  @Column( name = "mimetype" )
  private String            d_strMimeType;

  /**
   * Width of the image in pixel.
   */
  @Column( name = "width" )
  private int               d_nWidth;

  /**
   * Height of the image in pixel.
   */
  @Column( name = "height" )
  private int               d_nHeight;

  /**
   * Time of the image creation.
   */
  @Column( name = "create_time" )
  private GregorianCalendar d_calCreateTime;

  /**
   * Type of the chart.
   */
  @Enumerated( EnumType.STRING )
  @Column( name = "type" )
  private GChartType        d_type;

  /**
   * The club which is possibly attached (may be null).
   */
  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "club_id" )
  private GClub                  d_club;

  /**
   * The matchday which is possibly attached (may be null).
   */
  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "matchday_id" )
  private GMatchday              d_matchday;

  /** Default no argument constructor. */
  protected GChart()
  {}

  /**
   * Creates a new chart with the given data.
   *
   * @param type the type of the created chart
   * @param abImage the chart image
   * @param strMimeType the image's MIME type
   * @param nWidth the image width in pixels
   * @param nHeight the image height in pixels
   */
  public GChart( GChartType type, byte[] abImage, String strMimeType, int nWidth, int nHeight )
  {
    d_type = type;
    d_data = abImage;
    d_strMimeType = strMimeType;
    d_nWidth = nWidth;
    d_nHeight = nHeight;
    d_calCreateTime = new GregorianCalendar();
  }

  /**
   * Returns the id.
   */
  public int getId()
  {
    return d_nId;
  }

  /**
   * Returns the raw image data.
   */
  public byte[] getData()
  {
    return d_data;
  }

  /**
   * Returns the image's content type, e.g. 'image/png'.
   */
  public String getMimeType()
  {
    return d_strMimeType;
  }

  public int getWidth()
  {
    return d_nWidth;
  }

  public int getHeight()
  {
    return d_nHeight;
  }

  public GregorianCalendar getCreateTime()
  {
    return d_calCreateTime;
  }

  public GChartType getType()
  {
    return d_type;
  }

  public GClub getClub()
  {
    return d_club;
  }

  public GMatchday getMatchday()
  {
    return d_matchday;
  }

  public void setClub( GClub club )
  {
    if ( d_club != null )
      throw new IllegalStateException( "The club of a chart can only be set once." );

    d_club = club;
  }

  public void setMatchday( GMatchday matchday )
  {
    if ( d_matchday != null )
      throw new IllegalStateException( "The matchday of a chart can only be set once." );

    d_matchday = matchday;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GChart.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GChart) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return String.valueOf( d_nId ) + " (" + d_type.name() + ")";
  }
}
